<?php

namespace App\Jobs;

use App\Http\Util\Wechat;

use App\Model\House;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class PushMessage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $order;
    public $tries = 1;
    public $timeout = 12;
    private $openid;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($order,$openid)
    {
        $this->order = $order;
        $this->openid =$openid;
        //因为要导入大文件 需要很久时间
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $token = Wechat::getAccessToken();
            $postData = [
                'touser' => $this->openid,
                'template_id' => '6KU5FXZLFqoV3XFZdu0HAF85ZfVYIneU8Nq4N9zMeTM',
                'data' => [
                    'amount1' => ['value' => $this->order->pay_amt],
                    'thing11' => ['value' => $this->order->house->estate_name.$this->order->house->name],
                    'character_string13' => ['value' => $this->order->device->sn],
                    'character_string6' => ['value' =>$this->order->order_no],
                    'time3' => ['value' => $this->order->paid_at]
                ]
            ];
            $postData =json_encode($postData);
            Log::info($postData);
            $result =  Wechat::sendMessage($postData,$token);
            Log::info($result);
        }catch (\Exception $e){
            Log::info($e->getMessage());
        }
    }
}

